//
//  Networking.swift
//  SwiftWeather
//
//  Created by shen on 2023/8/28.
//

import Foundation

enum WeatherHTTPBase {
    static let scheme = "https"
    static let host = "api.weatherapi.com"
}

enum WeatherAPIRoute {
    case forecast
    case search

    var apiVersion: String { "v1" }

    var path: String {
        switch self {
        case .forecast:
            return "/\(apiVersion)/forecast.json"
        case .search:
            return "/\(apiVersion)/search.json"
        }
    }
}


enum HTTPError: LocalizedError {
    case nonHTTPRequest
    case requestFailed(statusCode: Int)
    case serverError(statusCode: Int)
    case networkError(Error)
    case decodingError(DecodingError)

    var isRetriable: Bool {
        switch self {
        case .decodingError:
            return false
        case .requestFailed(let statusCode):
            return [408, 429].contains(statusCode)
        case .serverError, .networkError, .nonHTTPRequest:
            return true
        }
    }

    var errorDescription: String? {
        switch self {
        case .nonHTTPRequest:
            return "Non HTTP URL Request"
        case .requestFailed(let statusCode):
            return "Request failed with status code: \(statusCode)"
        case .serverError(let statusCode):
            return "Server error, status code: \(statusCode)"
        case .networkError(let error):
            return "\(error.localizedDescription)"
        case .decodingError(let decodingError):
            return "\(decodingError.localizedDescription)"
        }
    }
}
